1. 题目描述(中等难度)

*[warning] 78. 子集

2. 解法一: 回溯

class Solution {
    List<List<Integer>> resp = new ArrayList<>();
    List<Integer> ans = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
         backTracking(nums,0);
         return resp;
    }

    public void backTracking(int[] nums,int startIndex){
        resp.add(new ArrayList<>(ans));
        if(startIndex >= nums.length){
           return;
        }
        for(int i=startIndex;i<nums.length;i++){
            ans.add(nums[i]);
            backTracking(nums,i+1);
            ans.remove(ans.size()-1);
        }
    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""